import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from head.Init_Driver import SurveyExaminationSystem_Driver


class Home:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/login"
        self.driver = SurveyExaminationSystem_Driver.driver
        self.driver.get(self.url)
        # 初始化登录成功状态
        self.driver.find_element(By.ID,"username").send_keys("karen")
        self.driver.find_element(By.ID,"password").send_keys("karen1234")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()
        self.driver.maximize_window()
        # 检查是否登录成功
        # assert WebDriverWait(self.driver,5).until(
        #     expected_conditions.visibility_of_element_located((By.XPATH,"//a[h1='问卷系统']")))
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#sk-layout > div > div > section > div > header > div > div.ant-pro-global-header-right-content > div > div > div")))

    def HomeUIFuncTest(self):
        # 点击顶栏“设置”按钮
        self.driver.find_element(By.CSS_SELECTOR,"span[class='anticon anticon-setting ant-dropdown-trigger']").click()
        # 点击用户设置
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='用户设置']")))
        self.driver.find_element(By.XPATH,"//span[text()='用户设置']").click()
        # 查看是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='基本设置']")))
        SurveyExaminationSystem_Driver.Get_ScreenShot()
        # 再次点击顶栏“设置”
        self.driver.find_element(By.CSS_SELECTOR,"span[class='anticon anticon-setting ant-dropdown-trigger']").click()
        # 点击整体风格设置
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='整体风格设置']")))
        self.driver.find_element(By.XPATH,"//span[text()='整体风格设置']").click()
        # 检查是否弹出相关对话框
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[h3='主题色']")))
        SurveyExaminationSystem_Driver.Get_ScreenShot()
        # 点击刷新按钮退出“整体风格设置”对话框
        self.driver.refresh()
        # 点击首页进入页面
        self.driver.get("http://8.155.1.153:8081/home")
        # 检查界面元素是否存在且正常显示内容
        # 首页菜单
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CLASS_NAME,"pageHeaderContent___PWSiu")))
        # 点击创建问卷
        self.driver.find_element(By.LINK_TEXT,"创建问卷").click()
        # 检查是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#root > div > div.survey-main-navigator > div.actions-container > div:nth-child(2)")))
        time.sleep(2)
        SurveyExaminationSystem_Driver.Get_ScreenShot()
        # 回到首页
        self.driver.back()
        # 点击创建考试
        self.driver.find_element(By.LINK_TEXT,"创建考试").click()
        # 检查是否跳转至相关页面
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR, "#root > div > div.survey-main-navigator > div.actions-container > div:nth-child(2)")))
        time.sleep(2)
        SurveyExaminationSystem_Driver.Get_ScreenShot()
        # 回到首页
        self.driver.back()
        self.driver.refresh()
        # 测试首页详情框-我的考试
        assert self.driver.find_element(By.CSS_SELECTOR,"#rc-tabs-0-panel-exam > div > div > div > div.ant-pro-table-list-toolbar > div")
        assert self.driver.find_element(By.XPATH,"//tr[th='考试名称']")
        assert self.driver.find_element(By.XPATH, "//tr[th='开始时间']")
        assert self.driver.find_element(By.XPATH, "//tr[th='完成状态']")
        assert self.driver.find_element(By.XPATH, "//tr[th='操作']")
        # 测试首页详情框-我的问卷
        self.driver.find_element(By.XPATH,"//div[text()='我的问卷']").click()
        assert self.driver.find_element(By.CSS_SELECTOR,"#rc-tabs-0-panel-survey > div > div > div > div.ant-pro-table-list-toolbar > div")
        assert self.driver.find_element(By.XPATH,"//tr[th='问卷名称']")
        assert self.driver.find_element(By.XPATH, "//tr[th='开始时间']")
        assert self.driver.find_element(By.XPATH, "//tr[th='完成状态']")
        assert self.driver.find_element(By.XPATH, "//tr[th='操作']")

        # 测试首页详情框-问卷记录
        self.driver.find_element(By.XPATH,"//div[text()='问卷记录']").click()
        assert self.driver.find_element(By.CSS_SELECTOR,"#rc-tabs-0-panel-surveyHistory > div > div > div > div.ant-pro-table-list-toolbar > div")
        assert self.driver.find_element(By.XPATH,"//tr[th='问卷名称']")
        assert self.driver.find_element(By.XPATH, "//tr[th='我的答题时间']")
        assert self.driver.find_element(By.XPATH, "//tr[th='我的答题用时']")
        assert self.driver.find_element(By.XPATH, "//tr[th='操作']")

        # 测试首页详情框-考试记录
        self.driver.find_element(By.XPATH,"//div[text()='考试记录']").click()
        assert self.driver.find_element(By.CSS_SELECTOR,"#rc-tabs-0-panel-examHistory > div > div > div > div.ant-pro-table-list-toolbar > div")
        assert self.driver.find_element(By.XPATH,"//tr[th='考试名称']")
        assert self.driver.find_element(By.XPATH, "//tr[th='我的答题时间']")
        assert self.driver.find_element(By.XPATH, "//tr[th='我的答题用时']")
        assert self.driver.find_element(By.XPATH, "//tr[th='我的得分']")
        assert self.driver.find_element(By.XPATH, "//tr[th='操作']")


# home = Home()
# home.HomeUIFuncTest()
# home.driver.quit()